package org.vectrics.recruiter.xmlweb;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.calendar.RecruiterCalendarHelper;
import org.vectrics.common.util.DateSync;
import org.vectrics.common.util.StringUtils;
import org.vectrics.company.Company;
import org.vectrics.company.CompanyPerson;
import org.vectrics.company.CompanyPersonRoleType;
import org.vectrics.company.Sector;
import org.vectrics.dataweb.WebServiceAction;
import org.vectrics.domain.Domain;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.user.Interview;
import org.vectrics.skill.UserEvaluation;
import org.vectrics.struts.Request;
import org.vectrics.system.SystemConstant;
import org.vectrics.user.ContactInfo;
import org.vectrics.user.Person;
import org.vectrics.user.PersonCommunication;
import org.vectrics.user.PersonCommunicationMethod;
import org.vectrics.user.PersonCommunicationMethodType;
import org.vectrics.user.PersonEncounter;
import org.vectrics.user.PersonEncounterCriteria;
import org.vectrics.user.PersonPrivilegeGroup;
import org.vectrics.user.PersonPrivilegeGroupType;
import org.vectrics.user.User;
import org.vectrics.user.UserPrivilege;
import org.w3c.dom.Element;

/**
 * Respond to XML Service request to establish a new hosted site.
 * @author Mike Moore
 *
 */
public class RecruitCalendarWebService extends WebServiceAction {
	private transient Logger log = Logger.getLogger(this.getClass());
	public static String COMMAND = "get.person.calendar";
	public RecruitCalendarWebService() {
		super();
	}

	public void getData(Request request, String command, Element requestElement,
			Element responseElement) {
		ThreadProperties.setSystemUser(true);
		ThreadProperties.setPerson(CoreServiceLocator.getConfiguration().getSystemPerson());
		
		log.info("Command: " + command);
		if (command.equals(COMMAND)) {
			log.info("Command to establish new site");

			String personId = request.getParameter("personId");
			if (StringUtils.isNullOrBlank(personId)) {
				throw new SystemException("Parameter 'personId' not passed.");
			}
			Person person = CoreServiceLocator.getPersonService().findPersonById(new Long(personId));
			PersonEncounterCriteria criteria = new PersonEncounterCriteria();
			criteria.setPerson(person);

			RecruiterCalendarHelper helper = new RecruiterCalendarHelper();
			Element dataElement = this.getDataElement(responseElement);
			List encounters = CoreServiceLocator.getPersonService().findPersonEncounters(criteria);
			Iterator iter = encounters.iterator();
			while (iter.hasNext()) {
				PersonEncounter encounter = (PersonEncounter)iter.next();
				log.info("Adding encounter: " + encounter);
				Element encounterElement = responseElement.getOwnerDocument().createElement("personExpirable");
				dataElement.appendChild(encounterElement);
				helper.personExpirableToXml(encounter, encounterElement, request.getLocale());
			}
		} else {
			throw new SystemException("Do not know how to handle command: " + command);
		}
		
	}

	/**
	 * Find a unique domain name (a name not in the database) that begins with the name passed to this method.
	 * @param companyName
	 * @return the unique name.
	 */
	private String findUniqueDomainName(String companyName) {
		log.info("See if domain already exists, if so adjust the domain name and test again until a new name is found");
		String adjustedName = companyName;
		int suffix = 1;
		boolean doneTesting = false;
		while (!doneTesting) {
			Domain domain = CoreServiceLocator.getSystemService().findDomainForName(adjustedName);
			if (domain == null) {
				log.debug("Domain name is unique, starting");
				doneTesting = true;
			} else {
				adjustedName = companyName + " - " + suffix;
			}
			suffix++;
		}
		log.info("Requested domain name: " + companyName + "  Adjusted name: " + adjustedName);
		return(adjustedName);
	}	
	
	
	private void setDefaultSystemValuesForNewSite(Domain domain) {
		//SystemConstant systemConstant = CoreServiceLocator.getSystemService().findSystemConstant(domain, "");
		
		
	}
	
	
}
